package util.core;

import com.oreilly.servlet.multipart.FileRenamePolicy;

import java.io.File;
import java.util.Date;
import java.util.Random;

/**
 * Created by WhiteBlue on 15/5/15.
 */
public class FileRenameUtil implements FileRenamePolicy {

    public File rename(File file) {
        String body = "";
        String ext = "";
        Date date = new Date();
        int pot = file.getName().lastIndexOf(".");
        if (pot != -1) {//说明后缀存在
            body = date.getTime() + "";//采用时间的形式命名
            ext = file.getName().substring(pot);
        } else {
            body = (new Date()).getTime() + "";
            ext = "";
        }
        String newName = body + new Random().nextInt(10) + ext;
        file = new File(file.getParent(), newName);
        return file;

    }

}
